package springboot.controller;

import com.alibaba.fastjson.JSON;
import springboot.http.HttpClientTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springboot.bean.Status;
import springboot.bean.StatusExample;
import springboot.mapper.StatusMapper;
import springboot.service.StatusService;
import springboot.webservicers.model.Student;
import springboot.webservicews.model.User;
import springboot.websocket.WebSocketServer;

import java.io.IOException;
import java.util.List;

@Controller
public class DoorController {

    @Autowired
    StatusService statusService;

    @Autowired
    StatusMapper statusMapper;

    Integer flag=1;
    /*@RequestMapping("/show")
    public String show() throws Exception {
        if (flag==1){
            //创建tcp/ip服务器
            new NettySer().start(10043);
            flag=0;
        }
        return "show";
    }*/
    /*@RequestMapping(value = "/open_door",method = RequestMethod.POST)//用ajax解决此类问题
    public String open_door(){
        SimpleServerHandler.close_door=false;
        SimpleServerHandler.open_door=true;
        System.out.println("开门的指令是"+SimpleServerHandler.open_door+"关门的指令是"+SimpleServerHandler.close_door);
        return "show";
    }
    @RequestMapping(value = "/close_door",method = RequestMethod.POST)
    public String close_door(){
        SimpleServerHandler.open_door=false;
        SimpleServerHandler.close_door=true;
        System.out.println("开门的指令是"+SimpleServerHandler.open_door+"关门的指令是"+SimpleServerHandler.close_door);
        return "show";
    }*/
    /*@ResponseBody注解的作用是将controller的方法返回的对象
    通过适当的转换器 转换为指定的格式之后，写入到response对象的body区（响应体中）
    在使用此注解之后不会再走视图处理器，而是直接将数据写入到输入流中，
    它的效果等同于通过response对象输出指定格式的数据*/
    /*要通过@ResponseBody 注解 将返回的json字符串放入响应体中，
    然后在前台js才能拿到json字符串进行解析，如果不加，响应体中就没有放入json字符串，
    前台自然是拿不到数据的，希望大家别理解错*/
   /* @RequestMapping(value = "/list",method = RequestMethod.GET)
    public @ResponseBody
    List returnList(){
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        return list;
    }
    @RequestMapping(value = "/stringGet",method = RequestMethod.GET)
    @ResponseBody
    public List returnStringList(){
        ArrayList<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        list.add("ddd");
        list.add("eee");
        list.add("fff");
        list.add("ggg");
        return list;
    }*/

    @RequestMapping("/showList")
    @ResponseBody
    public void returnBoxList() throws IOException {
        Integer maxLine=statusMapper.selectMaxLine(1);
        for (Integer column=1;column<=9;column++){
            if(maxLine==null){
                System.out.println("将货物存放在该列的第一个");
                /*这里写该列第一个变换的操作*/
                StatusExample ex=new StatusExample();
                StatusExample.Criteria criteria=ex.createCriteria();
                criteria.andColumnEqualTo(column);
                criteria.andLineEqualTo(1);
                Status status=new Status();
                status.setStatus("TOUSE");
                statusMapper.updateByExampleSelective(status,ex);
                /*货位确定了直接return出去了*/
                /*return之前直接把list发射出去，example实际上就是查询条件*/
                StatusExample ex1=new StatusExample();
                List<Status> list=statusMapper.selectByExample(ex1);
                WebSocketServer.sendInfo(list);
                return;
            }else if (maxLine<5){
                System.out.println("将货物存放在该列的下一个");
                StatusExample example=new StatusExample();
                StatusExample.Criteria criteria=example.createCriteria();
                criteria.andColumnEqualTo(column);
                criteria.andLineEqualTo(maxLine+1);
                Status status=new Status();
                status.setStatus("TOUSE");
                statusMapper.updateByExampleSelective(status,example);
                /*货位确定了直接return出去了*/
                /*return之前直接把list发射出去，example实际上就是查询条件*/
                StatusExample ex1=new StatusExample();
                List<Status> list=statusMapper.selectByExample(ex1);
                WebSocketServer.sendInfo(list);
                return;
            }else if(maxLine==5){
                System.out.println("直接进入下一列");
                /*只有在进入下一行的时候才进行另一个条件的查询*/
                maxLine=statusMapper.selectMaxLine(column+1);
            }
        }
        System.out.println("直接进入下一列");
        StatusExample ex1=new StatusExample();
        List<Status> list=statusMapper.selectByExample(ex1);
        WebSocketServer.sendInfo(list);
    }

    @RequestMapping("/changeListToNO")
    @ResponseBody
    public void changeList(@RequestParam(value = "index")int index) throws IOException {
        /*webservice服务端接口的调用*/
        // 创建动态客户端 实现一
        /*JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
        Client client = factory.createClient("http://localhost:9998/services/CommonService?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
        Object[] objects;
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("sayHello", "胖胖");
            System.out.println("返回类型：" + objects[0].getClass());
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }*/
        /*此方式有问题*/
        /*CommonService_Service c = new CommonService_Service();
        User user = c.getCommonServiceImplPort().getUser("Tom");
        System.out.println(user);*/
        /*方式三*/
        /*这个东西有问题，webservice创建的目的就是为了远程服务器之间的访问以及调用，结果你直接跟我来了个服务器的类
        * 直接把我给整不会了
        * */
        /*try {
            // 接口地址
            // 代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 设置代理地址
            jaxWsProxyFactoryBean.setAddress("http://localhost:9998/services/CommonService?wsdl");
            // 设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(ICommonService.class);
            // 创建一个代理接口实现
            ICommonService cs = (ICommonService) jaxWsProxyFactoryBean.create();
            // 数据准备
            String userName = "Leftso";
            // 调用代理接口的方法调用并返回结果
            String result = cs.sayHello(userName);
            System.out.println("返回结果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        /*httpClient实现和webservice服务端的交互*/
       /* String json=HttpClientTools.sendGetData("http://localhost:9998/api/test/getStudentById/86","utf-8");
        Student student=JSON.parseObject(json, Student.class);
        System.out.println(student);
        System.out.println(student.getId());
        System.out.println(student.getName());
        System.out.println(student.getAge());
        System.out.println("+++++++++++++++++++++++");
        User user1=student.getList().get(0);
        System.out.println(user1.getId());
        System.out.println(user1.getName());
        System.out.println(user1.getAge());
        System.out.println("=======================");
        User user2=student.getList().get(1);
        System.out.println(user2.getId());
        System.out.println(user2.getName());
        System.out.println(user2.getAge());*/

        int id=index+1;
        /*持久化事件*/
        StatusExample ex=new StatusExample();
        StatusExample.Criteria criteria=ex.createCriteria();
        criteria.andIdEqualTo(id);
        /*查找查询条件*/
        Status status=new Status();
        status.setStatus("NO");
        statusMapper.updateByExampleSelective(status,ex);
        /*发送更新后的事件*/
        HttpClientTools.sendGetData("http://localhost:9998/showList","utf-8");
    }

    @RequestMapping("/changeListToNOUSE")
    @ResponseBody
    public void changeListToNOUSE(@RequestParam(value = "index")int index) throws IOException {
        int id=index+1;
        /*查询条件*/
        StatusExample ex=new StatusExample();
        StatusExample.Criteria criteria=ex.createCriteria();
        criteria.andIdEqualTo(id);
        /*更新的内容*/
        Status status=new Status();
        status.setStatus("NOUSE");
        /*Selective是动态的sql语句*/
        statusMapper.updateByExampleSelective(status,ex);
        /*发送更新后的事件*/
        HttpClientTools.sendGetData("http://localhost:9998/showList","utf-8");
    }
    /*这里证明了mybatis映射的标准
    * 即数据库查询出的字段，应该与类中的字段名称一致，
    * 类中可以添加相关的字段名，数据库中可以起别名
    * */
    @RequestMapping("/groupBy")
    @ResponseBody
    public List returnGroupByList() throws IOException {
        List<Status> list=statusMapper.selectGroupByList();
        System.out.println(list);
        return list;
    }
}
